<?
// $_REQUEST= $_POST + $_GET
ini_set('include_path', '/home/tadams/classes/');
require_once('DefinedValueFacade.php');

$request= new DefinedValueFacade($_REQUEST);

$converter= $request->setConverter('date','Date');
$converter->setDefault(new DateTime());
$converter->setErrorMessage('Invalid date.');
$converter->setFormat('%d/%m/%Y');

$validator= $request->addValidator('date','Required');
$validator->setErrorMessage('Date has to be set.');
$validator= $request->addValidator('date','DateRange');
$validator->setErrorMessage('Date must be before today.');
$validator->setMax(new DateTime());

// default value
$errors= array();
if (isset($_REQUEST['submitted'])) {
	// conversion and validation
	if (!($errors= $request->getErrorMessages())) {
		$date= $request->getAsDatatype('date');
		// formatting
		echo('Sent date: '.$date->format('d/m/Y'));
	}
}

// INPUT mask
?>
<h2>Please enter following fields</h2>

<ul>
<? foreach ($errors as $error) { ?>
	<li><?= $error->getErrorMessage(); ?></li>
<? } ?>
</ul>

<form>
	<input type="hidden" name="submitted" value="true" />
	<span>Date:</span>
	<input type="text" name="date" value="<?= $request->getAsString('date') ?>" />
	<input type="submit" value="Submit" />
</form>
